import request from "@/utils/request.ts";
import { formatPaginatedParams, Pagination, formatPaginatedResult } from "@/utils/format";
/**
 * 用户中心相关接口
 * @author Finn Yu
 * @since 2023/7/5 16:18
 */
const BASE_PATH = '/usercenter';

export function getVerifyCode() {
  return request(`${BASE_PATH}/login/getVerifyCode`);
}

export async function login(params: any) {
  return request(`${BASE_PATH}/login`, {
    method: 'POST',
    params,
  });
}

//获取项目配置
export function getAppInfo() {
  return request(`${BASE_PATH}/getAppInfo`);
}

//获取当前用户
export async function getMySelf() {
  return request(`${BASE_PATH}/account/getMySelf`);
}

//获取我的菜单
export function getMyMenu() {
  return request(`${BASE_PATH}/menu/queryMyMenu`, {
    params: {
      type: 0,
    },
  });
}

//更新密码
export async function updatePassword(data: any) {
  return request(`${BASE_PATH}/account/updatePassword`, {
    method: 'POST',
    data,
  });
}


export interface UserDetail {
  id?: React.Key
  name?: string
  phone?: string
  departmentId?: number
  departmentName?: string
  zoneCode?: string
  zoneName?: string
  roleIds?: React.Key[]
  userName?: string
  validTime?: string
  userPassword?: string
  accountType?: number
  typeRelevanceId?: React.Key
}

export interface UserSearchParams
  extends Pick<UserDetail, 'name' | 'phone' | 'departmentId' | 'zoneCode'> {
  roleId?: React.Key
}

// 获取用户列表
export const getUserList = async (params: Pagination & UserSearchParams) => {
  return request(`${BASE_PATH}/account/queryPage`, {
    method: 'GET',
    params: formatPaginatedParams(params, 'page', 'pageSize')
  }).then((res) => formatPaginatedResult(res))
}

// 获取用户详情
export const getUserDetail = async (accountId?: React.Key) => {
  return request(`${BASE_PATH}/account/queryById`, {
    method: 'GET',
    params: { accountId }
  })
}

// 新增或修改帐号
export const addOrModifyUser = async (data?: UserDetail) => {
  return request(`${BASE_PATH}/account/addOrModify`, {
    method: 'POST',
    data
  })
}

// 删除帐号
export const deleteUser = async (accountId?: React.Key) => {
  return request(`${BASE_PATH}/account/deleteById`, {
    method: 'GET',
    params: { accountId }
  })
}

export const queryLoginRecordTableData = async (params?: {
  startTime?: string | number
  endTime?: string | number
  userName?: string | number
  realName?: string | number
  zoneCode?: string | number
  departmentId?: string | number
  current: number
  size: number
}) => {
  return request(`${BASE_PATH}/account/loginRecord`, {
    method: 'GET',
    params: formatPaginatedParams(params)
  }).then((res) => formatPaginatedResult(res))
}


// 获取角色列表
export const getRoleList = async () => {
  return request(`${BASE_PATH}/role/queryAll`)
}

// 删除角色里的用户
export const deleteRoleUser = async (params?: {
  accountId?: React.Key
  roleId?: React.Key
}) => {
  return request(`${BASE_PATH}/role/deleteAccountRole`, {
    method: 'GET',
    params
  })
}

// 分配用户
export const assignUser = async (params?: {
  accountIds?: React.Key[]
  roleId?: React.Key
}) => {
  return request(`${BASE_PATH}/role/addAccountRole`, {
    method: 'GET',
    params
  })
}

// 新增或修改角色
export const addOrModifyRole = async (data?: {
  id?: React.Key
  roleName?: string
}) => {
  return request(`${BASE_PATH}/role/addOrModify`, {
    method: 'POST',
    data
  })
}

// 删除角色
export const deleteRole = async (roleId: React.Key) => {
  return request(`${BASE_PATH}/role/deleteById`, {
    method: 'GET',
    params: { roleId }
  })
}

// 新增角色菜单
export const addRoleMenu = async (params?: {
  menuIds: React.Key[] | string
  roleId?: React.Key
  type?: React.Key
}) => {
  return request(`${BASE_PATH}/role/addRoleMenu`, {
    method: 'GET',
    params
  })
}


interface RoleMenuProps {
  type: React.Key
  roleId?: React.Key
}

// 获取角色菜单
export const getRoleMenu = async (params: RoleMenuProps) => {
  return request('/usercenter/menu/queryByRoleId', {
    method: 'GET',
    params
  })
}
